package cn.wilson.hrm.service.impl;

import cn.wilson.hrm.domain.Sso;
import cn.wilson.hrm.domain.VipBase;
import cn.wilson.hrm.dto.RegisterDTO;
import cn.wilson.hrm.feign.client.RedisFeignClient;
import cn.wilson.hrm.mapper.SsoMapper;
import cn.wilson.hrm.mapper.VipBaseMapper;
import cn.wilson.hrm.service.ISsoService;
import cn.wilson.hrm.utils.AjaxResult;
import cn.wilson.hrm.utils.encrypt.MD5;
import cn.wilson.hrm.vo.SendSmsCodeVo;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

/**
 * <p>
 * 会员登录账号 服务实现类
 * </p>
 *
 * @author wilson.liu
 * @since 2020-04-29
 */
@Service
public class SsoServiceImpl extends ServiceImpl<SsoMapper, Sso> implements ISsoService {

    @Autowired
    private RedisFeignClient redisFeignClient;

    @Autowired
    private VipBaseMapper vipBaseMapper;



    @Override
    public void register(RegisterDTO registerDTO) {
        //基本参数判断
        //手机号
        String mobile = registerDTO.getMobile();
        //密码
        String password = registerDTO.getPassword();
        //手机验证码
        String smsCode = registerDTO.getSmsCode();
        //公司里判断要一个一个判断  细一点
        if (StringUtils.isBlank(mobile) || StringUtils.isBlank(password) || StringUtils.isBlank(smsCode)){
            throw new RuntimeException("无效的参数");
        }



        //判断手机号是否已经被注册
        Sso ssoInSql = baseMapper.selectByPhone(mobile);

        if (ssoInSql != null){
            throw new RuntimeException("手机号已被注册");
        }
        //判断手机验证码
        //存入redis中的key通过前缀加手机号
        String smsCodeKey = "REGISTER:SMS:" + mobile;
        AjaxResult ajaxResult = redisFeignClient.get(smsCodeKey);
        if (!ajaxResult.isSuccess() || ajaxResult.getResultObj() == null){
            throw new RuntimeException("验证码获取失败，请重新发送");
        }
        //从redis中获取手机验证码
        SendSmsCodeVo sendSmsCodeVo = JSON.parseObject(ajaxResult.getResultObj().toString(), SendSmsCodeVo.class);
        if (!sendSmsCodeVo.getSmsCode().equals(smsCode)){
            throw new RuntimeException("手机验证码错误,请重新发送");
        }


        //保存数据到t_sso
        Sso sso = new Sso();
        sso.setPhone(mobile);
        //随机盐
        String salt = MD5.getRandomCode(16);
        String md5pwd = MD5.getMD5(password + salt);
        sso.setPassword(md5pwd);
        sso.setSalt(salt);
        long nowTime = new Date().getTime();
        sso.setCreateTime(nowTime);
        baseMapper.insert(sso);

        //保存关联表 t_vip_base
        VipBase vipBase = new VipBase();
        vipBase.setCreateTime(nowTime);
        vipBase.setSsoId(sso.getId());
        vipBase.setRegChannel(1);
        vipBase.setRegTime(nowTime);

        vipBaseMapper.insert(vipBase);

    }


    /**
     *      登录
     * @param sso
     */
    @Override
    public String login(Sso sso) {
        //判断参数
        if (StringUtils.isBlank(sso.getPhone()) || StringUtils.isBlank(sso.getPassword())){
            throw new RuntimeException("用户名或密码不能为空");
        }

        //根据用户名查询mysql中的用户信息
        Sso ssofromMysql = baseMapper.selectByPhone(sso.getPhone());
        if (ssofromMysql == null){
            //根据用户名没查询到数据
            throw new RuntimeException("用户名没有找到");
        }

        //判断密码是否正确
        String md5pwd = MD5.getMD5(sso.getPassword() + ssofromMysql.getSalt());
        if (!ssofromMysql.getPassword().equals(md5pwd)){
            throw new RuntimeException("密码错误");
        }

        //生成redis的key
        String token = UUID.randomUUID().toString().replaceAll("-", "");

        //把登录信息放到redis 过期时间到了重新登录
        AjaxResult ajaxResult = redisFeignClient.setex(token, 60 * 60 * 24, JSON.toJSONString(ssofromMysql));
        //判断保存成功没有
        if (!ajaxResult.isSuccess()){
            throw new RuntimeException("登录失败，请重新登录");
        }

        //返回redis的key给浏览器
        return token;

    }
}
